<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/9
 * Time: 9:30
 */
namespace App\Http\Controllers\Api;

use App\Constant\ErrorConstant;
use App\Service\Api\SundryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SundryController extends BaseController
{
    protected $service;

    public function __construct(Request $request, SundryService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 热门搜索词列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function hotSundry()
    {
        [$flag, $result]    =   $this->service->sundryHot();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_HISTORY_HOT_ERROR, $result);
        }
    }

    /**
     * 专题页面模板内容
     * @return \Illuminate\Http\JsonResponse
     */
    public function templateSundry()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string',
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_SUNDRY_TEMPLATE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->sundryTemplate($this->request->post('template_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_SUNDRY_TEMPLATE_ERROR, $result);
        }
    }

}
